import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_novel/detail/bloc/home_commits_list_bloc.dart';
import 'package:flutter_novel/detail/page/home_commits_list.dart';
import 'package:flutter_novel/detail/page/home_detail_page.dart';
import 'package:flutter_novel/routers/navigator_utils.dart';
import 'package:flutter_novel/routers/routers.dart';
import 'package:flutter_novel/series_list/bloc/series_list_bloc.dart';
import 'package:flutter_novel/series_list/page/series_list_page.dart';

class HomeDetailNavigator {
  HomeDetailNavigator._();

  static Future toHomeDetailPage(BuildContext context) {
    return NavigatorUtils.push(
      context,
      _HomeRoutes.homeDetailPage,
      replace: false,
      clearStack: false,
    );
  }

  static Future toSeriesPage(BuildContext context) {
    return NavigatorUtils.push(
      context,
      _HomeRoutes.seriesListPage,
      replace: false,
      clearStack: false,
    );
  }

  static Future toCommitsPage(BuildContext context) {
    return NavigatorUtils.push(
      context,
      _HomeRoutes.homeCommitsListPage,
      replace: false,
      clearStack: false,
    );
  }

  static ModuleRouter getRouter() {
    return _HomeRoutes();
  }
}

class _HomeRoutes extends ModuleRouter {
  static const homeDetailPage = 'page/detail/page/home_detail_page';
  static const homeCommitsListPage = 'page/detail/page/home_commits_list';
  static const seriesListPage = 'series_list/page/series_list_page';

  @override
  void init(FluroRouter router) {
    router.define(
      homeDetailPage,
      handler: Handler(
        handlerFunc: (context, params) {
          Map? argument = context!.settings!.arguments as Map?;
          String imageUrl = argument?['imageUrl'] ?? '';
          return HomeDetailPage(imageUrl: imageUrl);
        },
      ),
    );

    router.define(
      seriesListPage,
      handler: Handler(
        handlerFunc: (context, params) {
          return BlocProvider<SeriesListBloc>(
            create: (context) => SeriesListBloc(),
            child: SeriesListPage(),
          );
        },
      ),
    );

    router.define(
      homeCommitsListPage,
      handler: Handler(
        handlerFunc: (context, params) {
          return BlocProvider<HomeCommitsListBloc>(
            create: (context) => HomeCommitsListBloc(),
            child: HomeCommitsListPage(),
          );
        },
      ),
    );
  }
}
